
import random, sys, time, re
from zoom import zoomer
from dzstore import Model

class Registration(Model):

    def errors(self):
        def verify(valid,message):
            if not valid:
                errors.append(message)

        errors = []
        verify(self.first_name,'First name missing')
        verify(self.last_name,'Last name missing')
        verify(self.phone,'Provide phone number where you can be reached')
        verify(self.email,'email address missing')
        verify(re.match('^[a-zA-Z0-9.@]+$',self.username),'Username must contain only letters and numbers')
        verify(len(self.username) >= zoomer.MIN_USERID_SIZE ,'Username must be at least %d characters' % zoomer.MIN_USERID_SIZE)
        verify(len(self.password) >= zoomer.MIN_PASSWD_SIZE ,'Password must be at least %d characters' % zoomer.MIN_PASSWD_SIZE)
        verify(self.password == self.confirm,'Passwords do not match')
        verify(len(self.email) >= 5 ,'email address must be at least 5 characters')
        verify(self.email.find('@') != -1 and self.email.find('.') != -1,'Invalid email address')
        verify(not self.already_registered(),'email address is already registered')
        verify(not self.name_taken(),'Username is taken.  Please choose another.')

        return errors

    def expired(self):
        return time.time() > self.expiry

    def already_registered(self):
        return zoomer.db("select * from dz_users where email=%s",self.email) and 1 or 0

    def name_taken(self):
        return zoomer.db("select * from dz_users where loginid=%s",self.username) and 1 or 0

    def activate(self):
        import dzacl
        acl = dzacl.Acl(zoomer.db)
        acl.add_contact(self.username,self.password,self.first_name,self.last_name,self.email,self.phone,status='A')


